//
//  OrderDetailVC.m
//  Ufan
//
//  Created by yangqingluo on 15/12/28.
//  Copyright © 2015年 yangqingluo. All rights reserved.
//

#import "OrderDetailVC.h"
#import "OrderDetailProductCell.h"
#import "ProductVC.h"
#import "OrderNameCell.h"

@interface OrderDetailVC ()<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) NSArray *showArray;
@property (nonatomic, strong) UFOrderInfo *order;
@property (nonatomic, strong) UITableView *showTableView;

@end

@implementation OrderDetailVC

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    if (!self.showTableView.superview) {
        [self.view addSubview:self.showTableView];
    }

}

- (instancetype)initWithOrder:(UFOrderInfo *)order{
    self = [super init];
    if (self) {
        self.order = order;
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupBarButtonItem];
    
}

- (void)setupBarButtonItem{
    self.title = @"订单详情";
    
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 64, 44)];
    [backButton setImage:[UIImage imageNamed:@"nav_back.png"] forState:UIControlStateNormal];
    [backButton setImageEdgeInsets:UIEdgeInsetsMake(12, 0, 12, 48)];
    backButton.tag = 988;
    [backButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    [self.navigationItem setLeftBarButtonItem:backItem];
}

-(void)goBack{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma getter
- (UITableView *)showTableView{
    if (!_showTableView) {
        _showTableView= [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - STATUS_BAR_HEIGHT) style:UITableViewStylePlain];
        _showTableView.multipleTouchEnabled = YES;
        _showTableView.dataSource = self;
        _showTableView.delegate = self;
        _showTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_showTableView setBackgroundColor:[UIColor clearColor]];
    }
    
    return _showTableView;
}

- (NSArray *)showArray{
    if (!_showArray) {
        _showArray = @[@{@"headTitle":@"简介"},
                       @{@"headTitle":@"派送信息"},
                       @{@"headTitle":@"订单信息"}
                       ];
    }
    
    return _showArray;
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.showArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (section) {
        case 0:{
            return 1;
        }
            break;
            
        case 1:{
            return self.order.orderShips.count ? 3 : 2;
        }
            break;
            
        case 2:{
            return 4;
        }
            break;
            
        default:
            break;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (indexPath.section) {
        case 0:{
            NSString *OrderDetailProductCellIdentifier = @"OrderDetailProductCellIdentifier";
            
            OrderDetailProductCell *cell = (OrderDetailProductCell *)[tableView dequeueReusableCellWithIdentifier:OrderDetailProductCellIdentifier];
            
            if (!cell) {
                cell = [[OrderDetailProductCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:OrderDetailProductCellIdentifier];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                
//                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            }
            
            cell.order = self.order;
            
            
            return cell;
        }
            break;
            
        default:{
            if (indexPath.row == 0) {
                NSString *CellIdentifier = @"OrderNameCell";
                OrderNameCell *cell = (OrderNameCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                
                if (cell == nil) {
                    cell = [[OrderNameCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                }
                
                NSDictionary *dic = self.showArray[indexPath.section];
                
                cell.name = dic[@"headTitle"];
                
                return cell;
            }
            else{
                NSString *itemCellIdentifier = @"itemCellIdentifier";
                
                UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:itemCellIdentifier];
                
                if (!cell) {
                    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:itemCellIdentifier];
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                    cell.textLabel.font = [UIFont systemFontOfSize:14.0];
                    
                    cell.detailTextLabel.font = cell.textLabel.font;
                    cell.detailTextLabel.textColor = [UIColor lightGrayColor];
                    
                    
                    UIView *bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(15, cell.height - 0.5, tableView.width - 30, 0.5)];
                    bottomLineView.backgroundColor = UFCOLOR_GRAYLINE;
                    [cell.contentView addSubview:bottomLineView];
                }
                
                
                if (indexPath.section == 1) {
                    if (self.order.orderShips.count) {
                        UFShipInfo *ship = self.order.orderShips[0];
                        
                        if (indexPath.row == 1) {
                            
                            cell.textLabel.text = @"快递公司";
                            cell.detailTextLabel.text = ship.shipper;
                        }
                        else if (indexPath.row == 2){
                            cell.textLabel.text = @"快递单号";
                            cell.detailTextLabel.text = ship.shippedNum;
                        }
                    }
                    else{
                        cell.textLabel.text = @"暂无";
                    }
                }
                else if (indexPath.section == 2){
                    if (indexPath.row == 1) {
                        
                        cell.textLabel.text = @"订单编号";
                        cell.detailTextLabel.text = self.order.orderNum;
                    }
                    else if (indexPath.row == 2){
                        cell.textLabel.text = @"下单手机号";
                        cell.detailTextLabel.text = self.order.receivePhoneNum;
                    }
                    else if (indexPath.row == 3){
                        cell.textLabel.text = @"下单时间";
                        cell.detailTextLabel.text = stringFromDate([NSDate dateWithTimeIntervalSince1970:self.order.createDate]);
                    }
                }
                
                
                return cell;
            }
            
        }
            break;
    }
    
    return [UITableViewCell new];
}


#pragma mark - Table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:{
            return [OrderDetailProductCell tableView:tableView heightForRowAtIndexPath:indexPath];
        }
            break;
            
        case 1:
        case 2:{
            return indexPath.row == 0 ? [OrderNameCell tableView:tableView heightForRowAtIndexPath:indexPath]: 44;
        }
            break;
        default:
            break;
    }
    
    return 0;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    if (section == 0) {
//        return 0;
//    }
//    
//    return 60;
//}
//
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    if (section == 0){
//        return nil;
//    }
//    
//    UIView *contentView = [[UIView alloc] init];
//    
//    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 30, tableView.width - 30, 24)];
//    label.backgroundColor = [UIColor clearColor];
//    label.textColor = [UIColor blackColor];
//    label.font = [UIFont systemFontOfSize:16.0];
//    NSDictionary *dic = self.showArray[section];
//    label.text = dic[@"headTitle"];
//    [contentView addSubview:label];
//    
//    UIView *bottomLineView1 = [[UIView alloc] init];
//    bottomLineView1.backgroundColor = [UIColor blackColor];
//    [contentView addSubview:bottomLineView1];
//    
//    UIView *bottomLineView2 = [[UIView alloc] init];
//    bottomLineView2.backgroundColor = [UIColor blackColor];
//    [contentView addSubview:bottomLineView2];
//    
//    bottomLineView1.frame = CGRectMake(15, 56, tableView.width - 30, 2);
//    bottomLineView2.frame = CGRectMake(15, 59, tableView.width - 30, 1);
//    
//    return contentView;
//}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        //订单信息中没有商品id，无法查看商品详情
//        UFProductInfo *productInfo = [UFProductInfo new];
//        
//        ProductVC *vc = [[ProductVC alloc]initWithProductInfo:productInfo];
//        [self.navigationController pushViewController:vc animated:YES];
    }
}


@end
